package com.skivingcloud.develop.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.exceptions.BusinessException;
import com.skivingcloud.common.utils.PageParameter;
import com.skivingcloud.common.utils.ReturnT;
import com.skivingcloud.common.utils.UuidUtil;
import com.skivingcloud.develop.entity.BusinessCommonParam;
import com.skivingcloud.develop.entity.SysBusinessCommon;
import com.skivingcloud.develop.mapper.SysBusinessCommonMapper;
import com.skivingcloud.develop.service.SysBusinessCommonService;
import com.skivingcloud.workflow.api.entity.TaskParam;
import com.skivingcloud.workflow.api.feignclient.ProcessFeignService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 通用业务数据表(业务的表单数据） 服务层实现。
 *
 * @author skivingcloud
 * @since 2024-01-18
 */
@Service
public class SysBusinessCommonServiceImpl extends ServiceImpl<SysBusinessCommonMapper, SysBusinessCommon> implements SysBusinessCommonService {
    
    @Resource
    private ProcessFeignService processFeignService;
    /**
     * 分页查询通用业务数据表(业务的表单数据）
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<SysBusinessCommon> queryByPage(PageParameter<SysBusinessCommon> parameter){
        Page<SysBusinessCommon> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create();
        return this.page(page, queryWrapper);
    }
    
    /**
     * 通用业务数据表提交
     *
     * @param businessCommonParam 参数
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean submitBusinessCommonAdd(BusinessCommonParam businessCommonParam){
        SysBusinessCommon sysBusinessCommon = new SysBusinessCommon();
        sysBusinessCommon.setCreateTime(LocalDateTime.now());
        sysBusinessCommon.setCreateUser(Objects.requireNonNull(UserUtil.getCurrentUser()).getId());
        sysBusinessCommon.setId(UuidUtil.get32UUID());
        sysBusinessCommon.setDesignerCode(businessCommonParam.getDesignerCode());
        sysBusinessCommon.setDesignerId(businessCommonParam.getDesignerId());
        sysBusinessCommon.setFormData(businessCommonParam.getFormData().toString());
        sysBusinessCommon.setVersion(1L);
        this.save(sysBusinessCommon);
        TaskParam taskParam = businessCommonParam.getTaskParam();
        // 如果定义了工作流则会启动一个流程
        if(taskParam != null && StringUtils.isNotBlank(taskParam.getDefKey())){
            taskParam.setBusinessKey(sysBusinessCommon.getId());
            taskParam.setTenantId(UserUtil.getCurrentUser().getTenantId());
            taskParam.setBriefReport(UserUtil.getCurrentUser().getStaffName() + "-" + businessCommonParam.getDesignerName());
            taskParam.setUserId(Objects.requireNonNull(UserUtil.getCurrentUser()).getId());
            taskParam.setBusinessDesignerId(businessCommonParam.getDesignerId());
            taskParam.setBusinessDesignerCode(businessCommonParam.getDesignerCode());
            ReturnT<String> r = processFeignService.submitTask(taskParam);
            if(r.getCode() != 0){
                throw new BusinessException(r.getMessage());
            }
        }
        return true;
    }
    
    /**
     * 通用业务数据表修改
     *
     * @param businessCommonParam 参数
     * @return 结果
     */
    @Override
    public Boolean submitBusinessCommonUpdate(BusinessCommonParam businessCommonParam){
        SysBusinessCommon sysBusinessCommon = this.getById(businessCommonParam.getId());
        sysBusinessCommon.setModTime(LocalDateTime.now());
        sysBusinessCommon.setModUser(UserUtil.getCurrentUser().getId());
        sysBusinessCommon.setFormData(businessCommonParam.getFormData().toString());
        this.updateById(sysBusinessCommon);
        TaskParam taskParam = businessCommonParam.getTaskParam();
        // 如果定义了工作流则会提交流程
        if(taskParam != null && StringUtils.isNotBlank(taskParam.getDefKey())){
            ReturnT<String> r = processFeignService.submitTask(taskParam);
            if(r.getCode() != 0){
                throw new BusinessException(r.getMessage());
            }
        }
        return true;
    }
}
